//
//  GoodsDetailBuyCountView.m
//  BonweBuy
//
//  Created by liuxin on 2020/2/25.
//  Copyright © 2020 liuxin. All rights reserved.
//

#import "GoodsDetailBuyCountView.h"

@interface GoodsDetailBuyCountView ()<UITextFieldDelegate>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *addButton;
@property (nonatomic, strong) UIButton *subButton;
@end

@implementation GoodsDetailBuyCountView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews{
    UILabel *countLabel = [UILabel initWithTextColor:[UIColor blackColor] fontStr:UIPFRegularFont(15)];
    countLabel.text = @"购买数量";
    [self addSubview:countLabel];
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.height.mas_equalTo(kAppWidth_Mini(60));
        make.left.mas_equalTo(kAppWidth_Mini(40));
    }];
    
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [addButton setTitle:@"+" forState:UIControlStateNormal];
    [addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    addButton.titleLabel.font = UIPFRegularFont(18);
    //文字有点偏下 强制上移一点
    addButton.titleEdgeInsets = UIEdgeInsetsMake(kAppWidth_6(-2), 0, 0, 0);
    [addButton addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:addButton];
    [addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(kAppWidth_Mini(90));
        make.centerY.equalTo(countLabel.mas_centerY);
        make.right.mas_equalTo(-kAppWidth_Mini(30));
    }];
    self.addButton = addButton;
    
    UITextField *countTextField = [[UITextField alloc]init];
    countTextField.placeholder = @"";
    countTextField.text = @"1";
    countTextField.textColor = [UIColor blackColor];
    countTextField.textAlignment = NSTextAlignmentCenter;
    countTextField.font = UIPFRegularFont(15);
    countTextField.keyboardType = UIKeyboardTypeNumberPad;
    countTextField.layer.borderColor = [UIColor grayColor].CGColor;
    countTextField.layer.borderWidth = 1;
    countTextField.delegate = self;
    [countTextField addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self addSubview:countTextField];
    [countTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kAppWidth_Mini(190));
        make.centerY.equalTo(countLabel.mas_centerY);
        make.height.mas_equalTo(kAppWidth_Mini(90));
        make.right.equalTo(addButton.mas_left);
    }];
    self.countTextField = countTextField;
    
    UIButton *subButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [subButton setTitle:@"-" forState:UIControlStateNormal];
    [subButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    subButton.titleLabel.font = UIPFRegularFont(18);
    //文字有点偏下 强制上移一点
    subButton.titleEdgeInsets = UIEdgeInsetsMake(kAppWidth_6(-1), 0, 0, 0);
    [subButton addTarget:self action:@selector(subAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:subButton];
    [subButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(kAppWidth_Mini(90));
        make.centerY.equalTo(countLabel.mas_centerY);
        make.right.equalTo(self.countTextField.mas_left);
    }];
    self.subButton = subButton;
    
    [self addLineWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1) color:[UIColor lightGrayColor]];
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@"0"] && textField.text.length == 0) {
        return NO;
    }
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.text.length == 0) {
        textField.text = @"1";
    }
    
    if (self.maxCount > 0) {
        if (textField.text.integerValue > self.maxCount) {
            textField.text = [NSString stringWithFormat:@"%ld",self.maxCount];
        }
    }
    
    if (self.nowCount) {
        self.nowCount(textField.text.integerValue);
    }
}

#pragma mark - 可是实时监听文本编辑的变化
-(void)changedTextField:(UITextField *)textField{
    NSLog(@"值是---%@",textField.text);
}

#pragma mark - 增加按钮点击事件
-(void)addAction{
    NSInteger count = self.countTextField.text.integerValue + 1;
    if (self.maxCount > 0) {
        if (count <= self.maxCount) {
            
        }else{
            return;
        }
    }
    self.countTextField.text = [NSString stringWithFormat:@"%ld",count];
    if (self.nowCount) {
        self.nowCount(self.countTextField.text.integerValue);
    }
}

#pragma mark - 减少按钮点击事件
-(void)subAction{
    if (self.countTextField.text.integerValue == 1) {
        return;
    }else{
        self.countTextField.text = [NSString stringWithFormat:@"%ld",self.countTextField.text.integerValue - 1];
    }
    if (self.nowCount) {
        self.nowCount(self.countTextField.text.integerValue);
    }
}

-(void)setBuyCount:(NSInteger)buyCount{
    _buyCount = buyCount;
    self.countTextField.text = [NSString stringWithFormat:@"%ld",_buyCount];
}

@end
